package com.wizardOther.Items;

import Validator.Validator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.Model.AccountingdataModel;
import com.table.AccDataTableController;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import com.wizardOther.OtherScreensFramework;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 *
 * @author Vi
 */
public class AddNewAccountingDataController extends ValidationForm implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;
    @FXML
    private Button save;
    @FXML
    private Button cancel;
    @FXML
    private Button edit;
    @FXML
    private TextField name;
    @FXML
    private TextField city;
    @FXML
    private TextField zipCode;
    @FXML
    private TextField street;
    @FXML
    private TextField house;
    @FXML
    private TextField local;
    @FXML
    private TextField nip;
    @FXML
    private TextField phone;
    @FXML
    private ComboBox peyerType;
    @FXML
    private TextArea comment;
    @FXML
    private Label errorName;
    @FXML
    private Label errorCity;
    @FXML
    private Label errorZipCode;
    @FXML
    private Label errorStreet;
    @FXML
    private Label errorHouse;
    @FXML
    private Label errorLocal;
    @FXML
    private Label errorNip;
    @FXML
    private Label errorPhone;
    @FXML
    private Label errorPeyerType;
    @FXML
    private Label errorComment;
   
    private Integer selectedData;
    private AccDataTableController controller;
    

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.clear();
        inputs.add(name);
        inputs.add(city);
        inputs.add(zipCode);
        inputs.add(street);
        inputs.add(house);
        inputs.add(local);
        inputs.add(nip);
        inputs.add(phone);
        inputs.add(peyerType);
        inputs.add(comment);

        labels.clear();
        labels.add(errorName);
        labels.add(errorCity);
        labels.add(errorZipCode);
        labels.add(errorStreet);
        labels.add(errorHouse);
        labels.add(errorLocal);
        labels.add(errorNip);
        labels.add(errorPhone);
        labels.add(errorPeyerType);
        labels.add(errorComment);
        
    }
    
    private void clearForm(){
         this.selectedData=null;
         this.name.setText("");
         this.city.setText("");
         this.zipCode.setText("");
         this.street.setText("");
         this.house.setText("");
         this.local.setText("");
         this.nip.setText("");
         this.phone.setText("");
         this.peyerType.setValue("Wybierz typ płatnika");
         this.comment.setText("");
         
         makeList(10);
         setWrongTextFields(correctField);
    }
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        if(event.getSource()==save){
            validateForm();
        }
        if(event.getSource()==cancel){
            clearForm();
        } 
        if(event.getSource()==edit){
            showAccDataTableDialog();
            
           selectedData=controller.getSelectedData();
            
            if(this.selectedData!=null){
                loadData(selectedData);
            }
            
        }
    }
    
     private void loadData(Integer selectedData) {
         AccountingdataModel data = DaoInstanceEnum.INSTANCE.getDao().getAccountingDataById(selectedData);
        if (data != null) {
            this.name.setText(data.getName());
            this.city.setText(data.getCity());
            this.zipCode.setText(data.getZipNumber());
            this.street.setText(data.getStreet());
            this.house.setText(data.getHouseNumber());
            this.local.setText(data.getLocalNumber());
            this.nip.setText(data.getNipNumber());
            this.phone.setText(data.getPhone());
            this.peyerType.setValue(data.getPayerType());
            this.comment.setText(data.getComments());
        }
    }
    
   
    private void validateForm(){
        makeList(10);
        takeDateFromForm();
        setWrongTextFields(correctField);
        
        
        printValidator(correctField);
        if(isCorrect(correctField)){
          
            List<Boolean> goodRecord = Validator.checkAccountingDataCheckList(getData());
            setWrongTextFields(goodRecord);
            
            if(isCorrect(goodRecord)){
             
               try{ 
                    DAO dao = DaoInstanceEnum.INSTANCE.getDao();
                   if(selectedData!=null){
                       AccountingdataModel acc = getData();
                       acc.setAccountingDataId(selectedData);
                       dao.updateAccData(acc);
                       createPopup("Zapisano zmiany!");
                   }
                   else{
                        dao.addAccountingdata(getData());
                        createPopup("Dodano nowy pojazd!");
                        this.selectedData=null;
                   }
                  
                    clearForm();
               }
               catch(Exception e){
                   e.printStackTrace();
               }
            }
        }
        
    }
    
    private void takeDateFromForm(){
        
        checkString(this.name.getText(),0);
        checkString(this.city.getText(),1);
        checkString(this.zipCode.getText(),2);
        checkString(this.street.getText(),3);
        checkString(this.house.getText(),4);
        checkString(this.local.getText(),5);
        checkNumer(this.nip.getText(),6, 10);
        checkInteger(this.phone.getText(),7);
        checkString(renamePeyerTypeToEnglish((String)this.peyerType.getValue()),8);
        checkComments(this.comment.getText(),9);
          
    }
    
   
    
    @FXML
    private void goToMain(java.awt.event.ActionEvent event) {
        myController.setScreen(OtherScreensFramework.OTHER_BUTTONS_PANEL);
    }
    
    private AccountingdataModel getData() {
        String name = this.name.getText();
        String city = this.city.getText();
        String zipCode = this.zipCode.getText();
        String street = this.street.getText();
        String house = this.house.getText();
        String local = this.local.getText();
        String nip = this.nip.getText();
        String phone = this.phone.getText();
        String peyerType = renamePeyerTypeToEnglish((String) this.peyerType.getValue());
        String comments = this.comment.getText();
        AccountingdataModel data = new AccountingdataModel(name, city, zipCode, street, house, local, nip, phone, peyerType);
        data.setComments(comments);

        return data;
    }
   
    
    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
    }

    private boolean showAccDataTableDialog() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("AccDataTable.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz dane");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            controller = loader.getController();
            controller.setDialogStage(dialogStage);
           // controller.setWizard(this);

            dialogStage.showAndWait();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

   
}
